# coding=utf-8
# author:xc
# datetime:2018/10/23 10:14

from . import api
from dbConnection.MysqlConn import Mysql
from app import util
from flask import request


@api.route('/api/news/title', methods=['POST', 'GET'])
def api_new_title():
    try:
        mysql = Mysql()
        sql = "select tid,type_name from news_type where status=1"
        res = mysql.getAll(sql, None)
        mysql.dispose()
        code = util.CODE_EMPTY
        if res:
            code = util.CODE_SUCCESS
    except Exception as e:
        print e
        res = []
        code = util.CODE_ERROR

    data = {
        'code': code,
        'data': res
    }
    return util.to_json(data)


@api.route('/api/news/list', methods=['POST', 'GET'])
def api_new_list():
    try:
        tid = request.values.get('tid')
        mysql = Mysql()
        sql = "select * from  news_list where news_type='%s'" % tid
        res = mysql.getAll(sql, None)
        mysql.dispose()
        code = util.CODE_EMPTY
        if res:
            code = util.CODE_SUCCESS

    except Exception as e:
        print e
        res = []
        code = util.CODE_ERROR
    data = {
        'code': code,
        'list': res
    }
    return util.to_json(data)
